#include "qae_global.h"
#include <QtCore/QMutex>
#include <QtCore/QSettings>
#include <QtCore/QStringList>
#include <QtCore/QTextCodec>
#ifdef Q_OS_WIN32
#include <windows.h>
#endif


namespace QAnsiEditor {


const char *QGlobal::FILE_SIGN = "ansi";
const char *QGlobal::MIME_ANSITEXT = "text/ansitext";
QGlobal *QGlobal::m_pInstance = 0;


QGlobal *QGlobal::instance()
{
    static QMutex mutex;
    if (!m_pInstance) {
        mutex.lock();
        if (!m_pInstance) {
            m_pInstance = new QGlobal;
        }
        mutex.unlock();
    }
    return m_pInstance;
}

QTextCodec *QGlobal::getEncodingCodec(Encoding encoding)
{
    switch (encoding) {
    case GBK:
        return QTextCodec::codecForName("GBK");
    case Big5:
        return QTextCodec::codecForName("Big5");
    case UTF8:
        return QTextCodec::codecForName("UTF-8");
    default:
        return 0;
    }
}

QGlobal::QGlobal()
{
    initSettings();
}

bool QGlobal::showGrid() const
{
    return m_bShowGrid;
}

void QGlobal::setShowGrid(bool bEnable)
{
    m_bShowGrid = bEnable;
}

bool QGlobal::autoFont() const
{
    return m_bAutoFont;
}

void QGlobal::setAutoFont(bool bEnable)
{
    m_bAutoFont = bEnable;
}

bool QGlobal::antiAlias() const
{
    return m_bAntiAlias;
}

void QGlobal::setAntiAlias(bool bEnable)
{
    m_bAntiAlias = bEnable;
}

QGlobal::Language QGlobal::language() const
{
    return m_language;
}

void QGlobal::setLanguage(Language lang) 
{
    m_language = lang;
}

QGlobal::Encoding QGlobal::inputEncoding() const
{
    return m_inputEncoding;
}

QTextCodec *QGlobal::inputEncodingCodec() const
{
    return QGlobal::getEncodingCodec(m_inputEncoding);
}

void QGlobal::setInputEncoding(Encoding encoding)
{
    m_inputEncoding = encoding;
}

QGlobal::Encoding QGlobal::outputEncoding() const
{
    return m_outputEncoding;
}

QTextCodec *QGlobal::outputEncodingCodec() const
{
    return QGlobal::getEncodingCodec(m_outputEncoding);
}

void QGlobal::setOutputEncoding(QGlobal::Encoding encoding)
{
    m_outputEncoding = encoding;
}

QString QGlobal::asciiFont() const
{
    return m_strAsciiFont;
}

void QGlobal::setAsciiFont(const QString &strAsciiFont)
{
    m_strAsciiFont = strAsciiFont;
}

QString QGlobal::nonAsciiFont() const
{
    return m_strNonAsciiFont;
}

void QGlobal::setNonAsciiFont(const QString &strNonAsciiFont)
{
    m_strNonAsciiFont = strNonAsciiFont;
}

QString QGlobal::escapeString() const
{
    return m_strEscape;
}

void QGlobal::setEscapeString(const QString &strEscape)
{
    m_strEscape = strEscape;
}

void QGlobal::initSettings()
{
    QSettings settings(QAE_APP_NAME, "global");
    if (settings.childKeys().isEmpty() && settings.childGroups().isEmpty()) {
        bool bShowGrid = false;
        bool bAutoFont = true;
        bool bAntiAlias;
        QString strEscape;
        Language language;
        Encoding inputEncoding;
        Encoding outputEncoding;
        QString strAsciiFont;
        QString strNonAsciiFont;
#ifdef Q_OS_WIN32
        bAntiAlias = false;
        strEscape = "\x1b";
        UINT uiACP = GetACP();
        if (uiACP == 936) {  // zh-cn
            language = Chinese_Simpilified;
            inputEncoding = GBK;
            outputEncoding = GBK;
            strAsciiFont = "Courier";
            strNonAsciiFont = "SimSun";
        } else if (uiACP == 950) {  // zh-tw
            language = Chinese_Traditional;
            inputEncoding = Big5;
            outputEncoding = Big5;
            strAsciiFont = "Courier";
            strNonAsciiFont = "MingLiU";
        } else {  // default
            language = English;
            // assume using GBK settings
            inputEncoding = GBK;
            outputEncoding = GBK;
            strAsciiFont = "Courier";
            strNonAsciiFont = "SimSun";
        }
#else
        bAntiAlias = true;
        strEscape = "\x1b\x1b";
        language = English;
        inputEncoding = UTF8;
        outputEncoding = UTF8;
        strAsciiFont = "Monospace";
        strNonAsciiFont = "WenQuanYi Zen Hei Mono";
#endif
        // Now, save all initial values
        settings.setValue("show_grid", bShowGrid);
        settings.setValue("auto_font", bAutoFont);
        settings.setValue("antialias", bAntiAlias);
        settings.setValue("esc_string", strEscape);
        settings.setValue("language", language);
        settings.setValue("input_encoding", inputEncoding);
        settings.setValue("output_encoding", outputEncoding);
        settings.setValue("ascii_font", strAsciiFont);
        settings.setValue("nonascii_font", strNonAsciiFont);
    }
}

void QGlobal::loadSettings()
{
    QSettings settings(QAE_APP_NAME, "global");
    m_bShowGrid = settings.value("show_grid").toBool();
    m_bAutoFont = settings.value("auto_font").toBool();
    m_bAntiAlias = settings.value("antialias").toBool();
    m_strEscape = settings.value("esc_string").toString();
    m_language = (Language)settings.value("language").toInt();
    m_inputEncoding = (Encoding)settings.value("input_encoding").toInt();
    m_outputEncoding = (Encoding)settings.value("output_encoding").toInt();
    m_strAsciiFont = settings.value("ascii_font").toString();
    m_strNonAsciiFont = settings.value("nonascii_font").toString();
}

void QGlobal::saveSettings()
{
    QSettings settings(QAE_APP_NAME, "global");
    settings.setValue("show_grid", m_bShowGrid);
    settings.setValue("auto_font", m_bAutoFont);
    settings.setValue("antialias", m_bAntiAlias);
    settings.setValue("esc_string", m_strEscape);
    settings.setValue("language", m_language);
    settings.setValue("input_encoding", m_inputEncoding);
    settings.setValue("output_encoding", m_outputEncoding);
    settings.setValue("ascii_font", m_strAsciiFont);
    settings.setValue("nonascii_font", m_strNonAsciiFont);
}


}  // namespace QAnsiEditor
